package com.mhl.campuscabinet;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{

    // 碎片
    RecmFragment recmFragment;
    SellFragment sellFragment;
    MessageFragment messageFragment;
    MineFragment mineFragment;

    // 碎片管理器
    FragmentManager fragmentManager;

    // 底部栏组件
    RadioGroup radioGroup;
    RadioButton rb_cbg;

    // 通信Handler
    Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 防止软键盘破坏布局
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_main);
        fragmentManager = getSupportFragmentManager();
        radioGroup = (RadioGroup)findViewById(R.id.rg_tab_bar);
        radioGroup.setOnCheckedChangeListener(this);
        // 初始化推荐页页被选中
        rb_cbg = (RadioButton)findViewById(R.id.page_cbg);
        rb_cbg.setChecked(true);
        // 处理handler的信息
        this.handler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                if (msg.what == 1) {
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    hideAllFragment(fragmentTransaction);
                    fragmentTransaction.show(recmFragment);
                    fragmentTransaction.commit();
                }
            }
        };
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideAllFragment(fragmentTransaction);
        switch (checkedId){
            case R.id.page_cbg:
                if(recmFragment == null){
                    recmFragment = new RecmFragment();
                    fragmentTransaction.add(R.id.ly_content,recmFragment);
                }else{
                    fragmentTransaction.show(recmFragment);
                }
                break;
            case R.id.page_sell:
                sellFragment = new SellFragment(handler);
                fragmentTransaction.add(R.id.ly_content,sellFragment);
                fragmentTransaction.show(sellFragment);
                break;
            case R.id.page_msg:
                if(messageFragment == null){
                    messageFragment = new MessageFragment();
                    fragmentTransaction.add(R.id.ly_content,messageFragment);
                }else{
                    fragmentTransaction.show(messageFragment);
                }
                break;
            case R.id.page_mine:
                if(mineFragment == null){
                    mineFragment = new MineFragment();
                    fragmentTransaction.add(R.id.ly_content,mineFragment);
                }else{
                    fragmentTransaction.show(mineFragment);
                }
                break;
        }
        fragmentTransaction.commit();
    }

    //隐藏所有Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction){
        if(recmFragment != null)fragmentTransaction.hide(recmFragment);
        if(sellFragment != null)fragmentTransaction.hide(sellFragment);
        if(messageFragment != null)fragmentTransaction.hide(messageFragment);
        if(mineFragment != null)fragmentTransaction.hide(mineFragment);
    }

    @Override
    protected void onResume() {
        super.onResume();

        int id = getIntent().getIntExtra("id", 0);
        System.out.println("回到主界面！！！！！！！！！！！！！！！！！！！！！！！！");
        if(id==2){
            // 初始化推荐页页被选中
            rb_cbg = (RadioButton)findViewById(R.id.page_sell);
            rb_cbg.setChecked(true);
        }
    }
}